package com.ruipengkj.model;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TPurchase;
import com.ruipengkj.bean.TSupplier;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.service.TEmployeeService;

public class PurchaseEnterModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private Vector<List<Object>> rowdatas=new Vector<List<Object>>();
	private Vector<String> columns = new Vector<String>();
	
	private TEmployeeService tEmployeeService;
	public PurchaseEnterModel(){
		tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		columns.add("进货单号");
//		columns.add("采购数量");
//		columns.add("采购金额");
		columns.add("采购员");
		columns.add("操作人");
		columns.add("供货商名称");
		columns.add("供货商手机");
		columns.add("供货商固话");
		columns.add("供货商联系人");
		columns.add("操作时间");
	}
	//获得行数
	public int getRowCount(){
		return rowdatas.size();
	}
	//获得列数
	public int getColumnCount(){
		return columns.size();
	}
	//获得单元格的值
	public Object getValueAt(int row,int column){
		
		return ((Vector)rowdatas.get(row)).get(column);
	}
	//获取列名
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//获取列对应的类型
//	public Class getColumnClass(int column){
//		return column == (columns.size()-1) ? ImageIcon.class : Object.class;
//	}
	//设置单元格是否可编辑
	public boolean isCellEditable(int rowIndex,int columnIndex){
		return false;
	}
	
	public void removeData(int row){
		rowdatas.remove(row);
	}
	
	
	
	//设置单元格的值
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		//取出对应的记录
		Vector rowdata = (Vector)rowdatas.get(rowIndex);
		//删除原来的值
		rowdata.remove(columnIndex);
		//添加用户新修改的值
		rowdata.add(columnIndex,aValue);
	}


	public void setRowData(List<TPurchase> tPurchaseList) throws Exception{
		rowdatas.clear();
		if(tPurchaseList!=null&&tPurchaseList.size()!=0){
			for(int i=0;i<tPurchaseList.size();i++){
				TPurchase TPurchase=tPurchaseList.get(i);
				List<Object> row=new Vector<Object>();
				row.add(TPurchase.getPurchaseID());//采购编号
//				row.add(TPurchase.getBuyer());//采购员
				TEmployee tEmployee1=null;//采购员
				if (TPurchase.getBuyer() !=null && !TPurchase.getBuyer().equals("")){
					tEmployee1 = tEmployeeService.getNameByIdService(TPurchase.getBuyer());
					if (tEmployee1 != null && tEmployee1.getName()!=null){
						row.add(tEmployee1.getName());
					}
				}
				if(tEmployee1==null){
					row.add("      ");
				}
				TEmployee tEmployee2=null;//操作人
				if (TPurchase.getOperator() !=null && !TPurchase.getOperator().equals("")){
					tEmployee2 = tEmployeeService.getNameByIdService(TPurchase.getOperator());
					if (tEmployee2 != null && tEmployee2.getName()!=null){
						row.add(tEmployee2.getName());
					}
				}
				if(tEmployee2==null){
					row.add("      ");
				}
				TSupplier supplier=TPurchase.getTSupplier();
				if(supplier!=null){
					row.add(supplier.getName());//供货商名称
					row.add(supplier.getMobile());//供货商手机
					row.add(supplier.getPhone());//供货商固话
					row.add(supplier.getContact());//供货商联系人
				}else{
					row.add("      ");//供货商名称
					row.add("      ");//供货商手机
					row.add("      ");//供货商固话
					row.add("      ");//供货商联系人
				}
				
				row.add(TPurchase.getHappenTime());//操作时间
				rowdatas.add(row);
			}
		}
	}

	public void clear(){
		rowdatas.clear();
	}
}
